package utils;


import java.util.Map;
import org.apache.log4j.Logger;

public class JointSql {
	private static Logger logger = Logger.getLogger(JointSql.class);

	/**
	 * sql拼接调用方法---返回语句
	 * 
	 * @param map
	 * @return
	 */
	public static String publicMethodSQL(Map<String, Object> map) {
		String type = map.get("type") + "";
		String table = map.get("table") + "";
		String sql = "";
		if ("S".equals(type)) {
			String files = map.get("files") + "";
			Object[] where = (Object[]) map.get("where");
			String tail = map.get("tail") + "";
			// 拼接方法
			sql = selectSQL(table, files, where, tail);
			// 查询方法
		} else if ("U".equals(type)) {
			Object[] where = (Object[]) map.get("where");
			Object[] valus = (Object[]) map.get("valus");
			// 拼接方法
			sql = updateSQL(table, where, valus);
			// 查询方法
		} else if ("D".equals(type)) {
			Object[] where = (Object[]) map.get("where");
			// 拼接方法
			sql = deleteSQL(table, where);
			// 查询方法
		} else if ("I".equals(type)) {
			Object[] valus = (Object[]) map.get("valus");
			// 拼接方法
			sql = insertSQL(table, valus);
		}
		return sql;
	}


	/**
	 * 查询sql拼接方法
	 * 
	 * @param table
	 *            表名
	 * @param files
	 *            查询字段
	 * @param where
	 *            条件
	 * @return
	 */
	public static String selectSQL(String table, String files, Object[] where, String tail) {
		String sql = "";
		String condition = "";
		if (tail == null || "".equals(tail) || "null".equals(tail)) {
			tail = "";
		}
		if (where.length == 0) {
			sql = "Select " + files + " from " + table;
		} else {
			for (int i = 0; i < where.length; i++) {
				condition += where[i] + " and ";
			}
			if (condition.endsWith("and ")) {
				condition = condition.substring(0, condition.length() - 4);
			}
			sql = "Select " + files + " from " + table + " where " + condition + " " + tail;
		}
		return sql;
	}

	/**
	 * 修改sql拼接方法
	 * 
	 * @param table
	 *            表明
	 * @param where
	 *            条件
	 * @param valus
	 *            修改字段
	 * @return
	 */
	public static String updateSQL(String table, Object[] where, Object[] valus) {
		String sql = "";
		String valusCondition = "";
		String whereCondition = "";
		if (where.length > 0 && valus.length > 0) {
			if (valus.length == 1) {
				valusCondition = valus[0] + "";
			} else {
				for (int i = 0; i < valus.length; i++) {//&&!valus[i].toString().contains("=''")
					if(!valus[i].toString().contains("=null")&&valus[i].toString().split("=").length!=1)
					valusCondition += valus[i] + ", ";
				}
				if (valusCondition.endsWith(", ")) {
					valusCondition = valusCondition.substring(0, valusCondition.length() - 2);
				}
			}
			for (int i = 0; i < where.length; i++) {
				whereCondition += where[i] + " and ";
			}
			if (whereCondition.endsWith("and ")) {
				whereCondition = whereCondition.substring(0, whereCondition.length() - 4);
			}
			sql = "update " + table + " set " + valusCondition + " where " + whereCondition;
		} else {
			logger.error("update\table:" + table + "无where条件");
		}
		return sql;
	}

	/**
	 * 删除方法
	 * 
	 * @param table
	 *            表名
	 * @param where
	 *            条件
	 * @return
	 */
	public static String deleteSQL(String table, Object[] where) {
		String sql = "";
		String condition = "";
		if (where.length > 0) {
			for (int i = 0; i < where.length; i++) {
				condition += where[i] + " and ";
			}
			if (condition.endsWith("and ")) {
				condition = condition.substring(0, condition.length() - 4);
			}
			sql = "Delete  from " + table + " where " + condition;
		} else {
			logger.error("Delete\table:" + table + "无where条件");
		}
		return sql;
	}

	/**
	 * 拼接插入语句
	 * 
	 * @param table
	 *            表明
	 * @param valus
	 *            条件 列：字段==>值
	 * @return
	 */
	public static String insertSQL(String table, Object[] valus) {
		String sql = "";
		String field = "";
		String condition = "";

		for (int i = 0; i < valus.length; i++) {
			String[] vaulsSples = (valus[i] + "").split("==>");
			if (vaulsSples.length != 1) {
				if (!"''".equals(vaulsSples[1])&& !"'null'".equals(vaulsSples[1])&& !"null".equals(vaulsSples[1])&&!"".equals(vaulsSples[1])) {
					field += vaulsSples[0] + ",";
					condition += vaulsSples[1] + ",";
				}
			}
		}
		if (field.endsWith(",")) {
			field = field.substring(0, field.length() - 1);
		}
		if (condition.endsWith(",")) {
			condition = condition.substring(0, condition.length() - 1);
		}
		sql = "Insert into " + table + "(" + field + ")values(" + condition + ")";

		return sql;
	}
}
